package com.axsoft.train;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;


/**
 * This class implements the cache manager
 * 
 */
class CacheManager
{
	/**
	 * Retrieves schedule from cache for given direction
	 */
	public static InputStreamReader getScheduleFromCache(String from, String to)
	{
		try
		{
			return new InputStreamReader(new FileInputStream(Constants.SCHED_CACHE_SD_PATH + from + "." + to), "utf8");
		} catch ( Exception e )
		{
			Log.e("CacheManager.getScheduleFromCache", e.toString());
			return null;
		}
	}
	
	
	/**
	 * Put schedule to cache
	 */
	public static boolean putScheduleToCache(String from, String to, String html) {
		try
		{
			new File(Constants.SCHED_CACHE_SD_PATH).mkdirs();
			
			FileOutputStream os = new FileOutputStream(Constants.SCHED_CACHE_SD_PATH + from + "." + to);
			OutputStreamWriter streamWriter = new OutputStreamWriter(os);
			
			streamWriter.write(html);
			
			streamWriter.flush();
			streamWriter.close();
			os.close();
		} catch ( Exception e )
		{
			Log.e("CacheManager.putScheduleToCache", e.toString());
			return false;
		}
		
		return true;
	}
	
	
	/**
	 * Splits ArrayList<String[2]> to ArrayList[2]<String>
	 */
	public static ArrayList<String[]> splitArrayList(ArrayList<String[]> array)
	{
		try
		{
			int size = array.size();
			if ( size == 0 )
				return null;
			
			String[] arr1 = new String[size];
			String[] arr2 = new String[size];
			
			for ( int i = 0; i < size; i++ )
			{
				String[] string = array.get(i);
				arr1[i] = string[0];
				arr2[i] = string[1];
			}
			
			ArrayList<String[]> outArray = new ArrayList<String[]>();
			outArray.add(arr1);
			outArray.add(arr2);
			
			return outArray;
			
		} catch ( Exception e )
		{
			Log.e("CacheManager.splitArrayList", e.toString());
			return null;
		}
	}
	
	
	/**
	 * Retrieves the directions list for the given city index
	 */
	public static ArrayList<String> getDirections(int index)
	{
		BufferedReader bufferedReader = MiscUtils.openFileForRead(Constants.DIR_CACHE_SD_PATH + String.valueOf(index));
		if ( bufferedReader == null )
			return null;
		
		ArrayList<String> directions = new ArrayList<String>();
		
		try
		{
			String string = null;
			do
			{
				string = bufferedReader.readLine();
				if (string != null && !string.startsWith("#") )
					directions.add(string);
			} while (string != null);
	
			bufferedReader.close();
			
		} catch ( Exception e )
		{
			Log.e("CacheManager.getDirections", e.toString());
		}
	
		return directions;
	}
	
	/**
	 * Gets raw text resource such as directions, stations by its name
	 */
	public static ArrayList<String[]> getRawTextArray(Context context, String resName)
	{
		if ( context == null || resName == null )
			return null;
		
		BufferedReader bufferedReader;
		
		try
		{
			Resources res = context.getResources();
			int id = res.getIdentifier(resName, "raw", context.getPackageName());
			if ( id == 0 )
				return null;
			InputStream is = res.openRawResource(id);
			bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF8"));
		} catch ( Exception e )
		{
			Log.e("CacheManager.getRawTextArray", e.toString());
			return null;
		}
		
		String string = null;
		String[] strArr = null;
		
		ArrayList<String[]> stations = new ArrayList<String[]>();
		
		do
		{
			try
			{
				string = bufferedReader.readLine();
				
				if (string != null) {
					strArr = string.split("::");
					if (strArr != null && strArr.length == 2) {
						stations.add(strArr);
					}
				}
			} catch ( Exception e )	{
				Log.e("CacheManager.getRawTextArray (1)", e.toString());
				break;
			}
		} while ( string != null );
		
		try
		{
			bufferedReader.close();
		} catch ( Exception e )
		{
			Log.e("CacheManager.getRawTextArray (2)", e.toString());
		}
		
		return stations;
	}
}